package com.jjx.service.impl;

import com.github.pagehelper.PageHelper;
import com.jjx.dao.IDepartmentMapper;
import com.jjx.domain.Department;
import com.jjx.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private IDepartmentMapper iDepartmentMapper;

    @Override
    public List<Department> findAll(Integer page,Integer limit,Department department) {
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if (department.getName()!=null && !"".equals(department.getName())){
            criteria.andLike("Name","%"+department.getName()+"%");
        }
        if (department.getArea()!=null && !"".equals(department.getArea())){
            criteria.andLike("area","%"+department.getArea()+"%");
        }
        if (department.getDescription()!=null && !"".equals(department.getDescription())){
            criteria.andLike("Description","%"+department.getDescription()+"%");
        }

        PageHelper .startPage(page,limit);
        return iDepartmentMapper.selectByExample(example);
    }

    @Override
    public List<Department> findAllDep(int id) {

        Department department = new Department();
        if (id != 0){
            department.setId(id);
        }

        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if (department.getId()!=null && !"".equals(department.getId())){
            criteria.andNotIn("Id", Collections.singleton(department.getId()));
        }
        return iDepartmentMapper.selectByExample(example);
    }

    //此方法实现递归查询
    //此方法主要功能：将所有的子部门信息赋值到父部门的children属性里面
    public void findAllChildren(List<Department> select) {
        for (Department department : select) {
            Integer id = department.getId();
            Department depa = new Department();
            depa.setParentDepartmentId(id);

            //获取子节点数据
            List<Department> sel = iDepartmentMapper.select(depa);

            //如果子节点数据不为空，则赋值到当前节点的children属性，并继续递归查询
            if(sel!=null && sel.size()>0)
            {
                department.setChildren(sel);
                findAllChildren(sel);
            }
        }

    }

    @Override
    public List<Department> findChildren(Integer id) {
        Department department = new Department();
        department.setParentDepartmentId(id);

        List<Department> select = iDepartmentMapper.select(department);
        findAllChildren(select);
        return select;
    }

    @Override
    public Department findByCondition(Department department) {
        return iDepartmentMapper.selectOne(department);
    }

    @Override
    public void save(Department department) {
        iDepartmentMapper.insert(department);
    }

    @Override
    public void update(Department department) {
        //更新属性不为空的值
        iDepartmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public void delete(int[] id) {
        for (int i : id) {
            iDepartmentMapper.deleteByPrimaryKey(i);
        }
    }
}
